package threads;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;

import domain.ClientDomainController;

public class AcceptStreamRequests extends Thread{

	private static final int ACCEPT_REQUEST_STREAM_PORT = 7777;
	private ClientDomainController dc;
	private DatagramSocket datagramSocket;
	private byte[] buffer;
	private DatagramPacket inPacket;

	public AcceptStreamRequests(ClientDomainController dc) {
		super("acceptStreamRequests");
		this.dc = dc;
	}

	@Override
	public void run() {
		super.run();
		while(true){
			try{
				datagramSocket = new DatagramSocket(ACCEPT_REQUEST_STREAM_PORT);
				buffer = new byte[1024];
				inPacket = new DatagramPacket(buffer, buffer.length);
				datagramSocket.receive(inPacket);
				
				InetAddress clientAddress = inPacket.getAddress();
				int clientPort = inPacket.getPort();
				
				String absolutePath = new String(inPacket.getData(), 0, inPacket.getLength());
				System.out.println("got stream request to send: " + absolutePath);
				dc.streamSongToServer(absolutePath);
				datagramSocket.close();
				
			}catch (IOException e) {
				System.out.println("IOException on send playlist: " + e.getMessage());
			}
		}
		
	}
	
	
	

}
